package com.mdoya.filter;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.mdoya.domain.Employee;
import com.mdoya.domain.User;
import com.mdoya.response.ResultResponse;
import com.mdoya.utils.ThreadLocalContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.dsig.spec.XPathType;
import java.io.IOException;

/**
 * @author mdoya
 * @version 1.0
 * @description: SpringMvc过滤器  注意：交给springboot管理，启动类配置 @ServletComponentScan SpringMvc扫描注解
 */
@Slf4j
@WebFilter("/*") // 拦截所有 注意：/* 正确用法   /** 错误用法
public class GlobaFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("Filter init :{}", filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
            // 1. 获取请求uri
        String requestURI = request.getRequestURI();
        log.info("请求路径uri: {}",requestURI);
        // 2. 公共资源需要放行
        if (requestURI.contains("login") || requestURI.contains("logout") ||requestURI.endsWith(".js") ||requestURI.endsWith(".css") || requestURI.contains("images")
                || requestURI.contains("element") || requestURI.contains("ico") || requestURI.contains("sendCode") || requestURI.contains("findTypeAll") || requestURI.contains("shoppingCart")){
            filterChain.doFilter(request,response);
            return;
        }

        // 3. 登录成功放行
        Employee employee = (Employee)request.getSession().getAttribute("employee");
        if (employee != null){
            System.out.println("当前线程id: " + Thread.currentThread().getId());
            log.info("登录成功: {}",requestURI);
            ThreadLocalContextUtils.setUserLongId(employee.getId());
            filterChain.doFilter(request,response);
            return;
        }
        // 用户登录成功放行
        User user = (User)request.getSession().getAttribute("user");
//        System.err.println("刚取到的user " + user);
        if (user != null){
//            System.err.println("you 用户登录" + user);
//            System.out.println("当前线程id: " + Thread.currentThread().getId());
            log.info("用户登录成功: {}",requestURI);
            ThreadLocalContextUtils.setUserLongId(user.getId());
            filterChain.doFilter(request,response);
            return;
        }

        // 前端用户放行
        if (requestURI.contains("front")){
            response.sendRedirect("/front/page/login.html");
            return;
        }

        // 4. 拦截响应
//        response.getWriter().write(JSON.toJSONString("NOTLOGIN"));
        response.sendRedirect("/backend/page/login/login.html");
    }

    @Override
    public void destroy() {
        log.debug("Filter destroy: {}", "过滤器销毁");
    }
}
